package bt.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import bt.excepcion.DAOExcepcion;
import bt.excepcion.LoginExcepcion;
import bt.modelo.Empresa;
import bt.util.ConexionBD;

public class EmpresaDAO extends BaseDAO {

	public void insertar(Empresa bt) throws DAOExcepcion {
		String query = "INSERT INTO Empresa(razonsocial,direccion,idpais,idDepartamento,idRubro,contacto,emailcontacto,sector,ruc,password,estado,fechaRegistro) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, bt.getRazonsocial());
			stmt.setString(2, bt.getDireccion());
			stmt.setInt(3, bt.getIdPais());
			stmt.setInt(4, bt.getIdDepartamento());
			stmt.setInt(5, bt.getIdRubro());
			stmt.setString(6, bt.getContacto());
			stmt.setString(7, bt.getEmailcontacto());
			stmt.setString(8, bt.getSector());
			stmt.setString(9, bt.getRuc());
			stmt.setString(10, bt.getPassword());
			stmt.setInt(11, bt.getEstado());
			stmt.setString(12, bt.getFechaRegistro());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void actualizar(Empresa bt) throws DAOExcepcion {
		// actualiza a la tabla empresa

		String query = "UPDATE Empresa SET razonsocial=?,direccion=?,idpais=?,idDepartamento=?,idRubro=?,contacto=?,emailcontacto=?,sector=?,ruc=?,password=?,estado=? WHERE idempresa=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, bt.getRazonsocial());
			stmt.setString(2, bt.getDireccion());
			stmt.setInt(3, bt.getIdPais());
			stmt.setInt(4, bt.getIdDepartamento());
			stmt.setInt(5, bt.getIdRubro());
			stmt.setString(6, bt.getContacto());
			stmt.setString(7, bt.getEmailcontacto());
			stmt.setString(8, bt.getSector());
			stmt.setString(9, bt.getRuc());
			stmt.setString(10, bt.getPassword());
			stmt.setInt(11, bt.getEstado());
			stmt.setInt(12, bt.getIdEmpresa());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void eliminar(int idEmpresa) throws DAOExcepcion {
		String query = "DELETE FROM Empresa WHERE idEmpresa=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idEmpresa);

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Empresa obtener(int idEmpresa) throws DAOExcepcion {
		Empresa bt = new Empresa();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "SELECT idempresa,razonsocial,direccion,idpais,idDepartamento,idRubro,contacto,emailcontacto,sector,ruc,password FROM Empresa WHERE idEmpresa=? AND estado=1";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idEmpresa);
			rs = stmt.executeQuery();
			if (rs.next()) {
				bt.setIdEmpresa(rs.getInt("idEmpresa"));
				bt.setRazonsocial(rs.getString("razonsocial"));
				bt.setDireccion(rs.getString("direccion"));
				bt.setIdPais(rs.getInt("idPais"));
				bt.setIdDepartamento(rs.getInt("idDepartamento"));
				bt.setIdRubro(rs.getInt("idRubro"));
				bt.setContacto(rs.getString("contacto"));
				bt.setEmailcontacto(rs.getString("Emailcontacto"));
				bt.setSector(rs.getString("Sector"));
				bt.setRuc(rs.getString("Ruc"));
				bt.setPassword(rs.getString("Password"));

			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return bt;
	}

	public Collection<Empresa> buscarPorNombre(String razonsocial)
			throws DAOExcepcion {
		String query = "SELECT idempresa,razonsocial,direccion,idpais,idDepartamento,idRubro,contacto,emailcontacto,sector,ruc,password FROM Empresa WHERE razonsocial like ? AND estado=1";
		Collection<Empresa> lista = new ArrayList<Empresa>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, "%" + razonsocial + "%");
			rs = stmt.executeQuery();
			while (rs.next()) {
				Empresa bt = new Empresa();
				bt.setIdEmpresa(rs.getInt("idEmpresa"));
				bt.setRazonsocial(rs.getString("razonsocial"));
				bt.setDireccion(rs.getString("direccion"));
				bt.setIdPais(rs.getInt("idPais"));
				bt.setIdDepartamento(rs.getInt("idDepartamento"));
				bt.setIdRubro(rs.getInt("idRubro"));
				bt.setContacto(rs.getString("contacto"));
				bt.setEmailcontacto(rs.getString("Emailcontacto"));
				bt.setSector(rs.getString("Sector"));
				bt.setRuc(rs.getString("Ruc"));
				bt.setPassword(rs.getString("Password"));

				lista.add(bt);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}

	public Collection<Empresa> listar() throws DAOExcepcion {
		Collection<Empresa> lista = new ArrayList<Empresa>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idempresa,razonsocial,direccion,idpais,idDepartamento,idRubro,contacto,emailcontacto,sector,ruc,password FROM Empresa";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {

				Empresa bt = new Empresa();
				bt.setIdEmpresa(rs.getInt("idEmpresa"));
				bt.setRazonsocial(rs.getString("razonsocial"));
				bt.setDireccion(rs.getString("direccion"));
				bt.setIdPais(rs.getInt("idPais"));
				bt.setIdDepartamento(rs.getInt("idDepartamento"));
				bt.setIdRubro(rs.getInt("idRubro"));
				bt.setContacto(rs.getString("contacto"));
				bt.setEmailcontacto(rs.getString("Emailcontacto"));
				bt.setSector(rs.getString("Sector"));
				bt.setRuc(rs.getString("Ruc"));
				bt.setPassword(rs.getString("Password"));

				lista.add(bt);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}

	public Collection<Empresa> buscarXrubro(int idRubro) throws DAOExcepcion {
		String query = "SELECT idempresa,razonsocial,direccion,idpais,idDepartamento,idRubro,contacto,emailcontacto,sector,ruc,password FROM Empresa WHERE idRubro=? AND estado=1";
		Collection<Empresa> lista = new ArrayList<Empresa>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idRubro);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Empresa bt = new Empresa();
				bt.setIdEmpresa(rs.getInt("idEmpresa"));
				bt.setRazonsocial(rs.getString("razonsocial"));
				bt.setDireccion(rs.getString("direccion"));
				bt.setIdPais(rs.getInt("idPais"));
				bt.setIdDepartamento(rs.getInt("idDepartamento"));
				bt.setIdRubro(rs.getInt("idRubro"));
				bt.setContacto(rs.getString("contacto"));
				bt.setEmailcontacto(rs.getString("Emailcontacto"));
				bt.setSector(rs.getString("Sector"));
				bt.setRuc(rs.getString("Ruc"));
				bt.setPassword(rs.getString("Password"));

				lista.add(bt);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return lista;
	}

	public Empresa validar(String correo, String clave) throws DAOExcepcion,
			LoginExcepcion {
		String query = "select idEmpresa, razonSocial from empresa where emailContacto=? and password=?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Empresa vo = new Empresa();
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, correo);
			stmt.setString(2, clave);
			rs = stmt.executeQuery();

			if (rs.next()) {
				vo.setIdEmpresa(rs.getInt("idEmpresa"));
				vo.setRazonsocial(rs.getString("razonSocial"));
			} else {
				throw new LoginExcepcion("No existe");
			}
		} catch (LoginExcepcion e) {
			System.err.println(e.getMessage());
			throw new LoginExcepcion(e.getMessage());
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}
}
